package blackeagle.ems.entities;

import blackeagle.ems.dal.DataClass;
import blackeagle.ems.dal.DataField;
import blackeagle.ems.dal.DataRecord;
import blackeagle.ems.dal.MethodType;
import blackeagle.ems.dal.Where;
import java.util.Date;

/**
 * A class representing a Game.
 */
@DataClass(dataName="GAME")
public class Game extends DataRecord<Game> {
    private Integer id, leadId;
    private Date startDate, deadline; 
    private String name;
    
    /**
     * The default 0-parameter constructor.
     */
    public Game(){}

    /**
     * A static class containing readonly strings with column names.
     */
    public static class Columns{
        public static final String Deadline = "deadline";
        public static final String Id = "id";
        public static final String LeadId = "leadid";
        public static final String Name = "name";
        public static final String StartDate = "startdate";
        
        private Columns(){}
    }
    
    @DataField(fieldName="deadline", methodType=MethodType.Getter)
    public Date getDeadline() {
        return deadline;
    }

    @DataField(fieldName="deadline", methodType=MethodType.Setter)
    public void setDeadline(Date deadline) {
        this.deadline = deadline;
        this._isDirty=true;
    }

    @DataField(fieldName="id", methodType=MethodType.Getter, isPrimary=true, autoIncrement=true)
    public Integer getId(){
        return this.id;
    }
    
    @DataField(fieldName="id", methodType=MethodType.Setter, isPrimary=true, autoIncrement=true)
    public void setId(Integer value){
        if(value>0){
            this.id=value;
            this._isDirty=true;
        }        
    }
    
    public Employee getLead(){
        Employee lead = new Employee();
        lead.load(this.leadId);
        
        return (lead.isNew()?null:lead);
    }
    
    public void setLead(Employee lead){
        Integer lId = lead.getId();
        this.setLeadId(lId);
    }

    @DataField(fieldName="leadid", methodType=MethodType.Getter)
    public Integer getLeadId() {
        return leadId;
    }

    @DataField(fieldName="leadid", methodType=MethodType.Setter)
    public void setLeadId(Integer leadId) {
        this.leadId = leadId;
        this._isDirty=true;
    }

    @DataField(fieldName="name", methodType=MethodType.Getter)
    public String getName() {
        return name;
    }

    @DataField(fieldName="name", methodType=MethodType.Setter)
    public void setName(String name) {
        this.name = name;
        this._isDirty=true;
    }

    @DataField(fieldName="startdate", methodType=MethodType.Getter)
    public Date getStartDate() {
        return startDate;
    }

    @DataField(fieldName="startdate", methodType=MethodType.Setter)
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
        this._isDirty=true;
    }
    
    /**
     * Attempts to load all the projects linked to this game.
     * @return the projects.
     */
    public ProjectCollection getProjects(){
        return new ProjectCollection()
                .where(new Where(Project.Columns.GameId, this.id))
                .load();
    }

    @Override
    public boolean equals(Object obj){
        if(obj instanceof Game){
            Game g = (Game)obj;
            return g.getId()==this.id;
        }
        
        return false;
    }
    
    @Override
    public String toString(){
        return this.name;
    }
}
